#include "MooSqlite.h"

MooSqlite::MooSqlite(string dbPath) {
    // 属性初始化
    _conn = 0;
    _isOpen = false;
    _errmsg = 0;
    
    if (open(dbPath)) {
        _isOpen = true;
    }
}

MooSqlite::~MooSqlite() {
    delete _errmsg;
    _errmsg = 0;
    
    // 关闭链接
    close();
}

void MooSqlite::close() {
    if (_isOpen) {
        sqlite3_close(_conn);
        _isOpen = false;
        setError("database is closed");
    }
}

bool MooSqlite::open(string dbPath) {
    if (_isOpen) {
        return false;
    }
    
    if (sqlite3_open(dbPath.c_str(), &_conn) != SQLITE_OK) {
        // 如果打开失败，则设置错误信息，并返回false
        setError(sqlite3_errmsg(_conn));
        return false;
    }
    
    return true;
}

bool MooSqlite::query(string sql) {
    if (!_isOpen) return false;
    
    if (sqlite3_exec(_conn, sql.c_str(), NULL, NULL, &_errmsg) != SQLITE_OK) {
        return false;
    }
    return true;
}

void MooSqlite::setError(string errmsg) {
    // 创建一个内存空间来保存信息
    char * tmp = new char[errmsg.length()];
    strcpy(tmp, errmsg.c_str());
    
    // 释放原来的空间，把指针指向新的内存
    delete _errmsg;
    _errmsg = tmp;
}

string MooSqlite::getError() {
    return string(_errmsg);
}

bool MooSqlite::getRow(RowResult *result, string sql) {
    SQLParam param;
    if (!getRow(result, sql, param)) {
        return false;
    }

    return true;
}

bool MooSqlite::getRow(RowResult *result, string sql, SQLParam param) {
    sql += " LIMIT 1";
    
    AllResult rs;
    if (!getAll(&rs, sql, param)) {
        return false;
    }
    
    // 取出结果的第一行进行返回
    *result = rs[0];
    return true;
}

bool MooSqlite::getCol(ColResult *result, string sql) {
    SQLParam param;
    if (!getCol(result, sql, param)) {
        return false;
    }
    
    return true;
}

bool MooSqlite::getCol(ColResult *result, string sql, SQLParam param) {
    AllResult rs;
    if (!getAll(&rs, sql, param)) {
        return false;
    }
    
    // 取出结果的第一列进行返回
    for (int i = 0; i < rs.size(); i++) {
        for (RowResult::iterator ite = rs[i].begin(); ite != rs[i].end(); ite++) {
            result->push_back(ite->second);
            break;
        }
    }
    
    return true;
}

bool MooSqlite::getAll(MooSqlite::AllResult * result, string sql) {
    SQLParam param;
    if (!getAll(result, sql, param)) {
        return false;
    }
    
    return true;
}

bool MooSqlite::getAll(MooSqlite::AllResult *result, string sql, SQLParam param) {
    if (!_isOpen) return false;
    
    // 用于存放结果集
    char **dbresult;
    
    int nrow, ncolumn;
    
    // 执行查询
    int ret = sqlite3_get_table(_conn, sql.c_str(), &dbresult, &nrow, &ncolumn, &_errmsg);
    if(ret != SQLITE_OK) {
        return false;
    }
    
    // 取出列信息
    map<int, string> colArr;
    for (int i = 0; i < ncolumn; i++) {
        colArr[i] = string(dbresult[i]);
    }
    
    // 取出行信息
    int index = ncolumn;
    for(int i = 0; i < nrow; i++) {
        RowResult row;
        for(int j = 0; j < ncolumn; j++) {
            row[colArr[j]] = dbresult[index++];
        }
        result->push_back(row);
    }
    
    sqlite3_free_table(dbresult);
    return true;
}



//string sql;

//    sql = "CREATE TABLE user(username VARCHAR(10), password VARCHAR(15))";
//    sql = "insert into user(username, password) values('mashunren', '123')";
//sql = "select * from user";

//sqlite3_close(db);

//cout << err << endl;
